package com.xiaomazi.acl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomazi.acl.dto.UserDTO;
import com.xiaomazi.acl.entity.User;
import com.xiaomazi.acl.mapper.UserMapper;
import com.xiaomazi.acl.service.UserService;
import com.xiaomazi.servicebase.exception.LLXYException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: llxy-parent
 * @description: 用户业务实现类
 * @author: 小马子
 * @create: 2023-03-14 13:21
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User selectByUsername(String username) {
        return this.query().eq("username",username).one();
    }

    /**
     * 根据条件查询信息并分页显示
     * @param userDTO 条件
     * @return Map of user
     */
    @Override
    public Map<String, Object> selectUserByCondition(UserDTO userDTO) {
        Map<String,Object> maps = new HashMap<>();
        if (userDTO == null) {
            throw new LLXYException(20001,"查询条件不能为空");
        }
        IPage<User> page = new Page<>(userDTO.getPage(),userDTO.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(userDTO.getUsername())){
            queryWrapper.like("username",userDTO.getUsername());
        }
        page = this.page(page, queryWrapper);
        maps.put("items",page.getRecords());
        maps.put("total",page.getTotal());
        return maps;
    }
}
